package maven.jyhs.com.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 动态代理类
 */
public class ProxyHandler implements InvocationHandler {
    Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        long stateTime = System.currentTimeMillis();
        Object invoke = method.invoke(target, args);
        long endTime = System.currentTimeMillis();
//        动态代理每个方法后加上这一句(如果想指定特定的方法执行而已可以获取method的getName,然后进行判断即可)
        System.out.println(method.getName() + "方法花费时间:" + (endTime - stateTime) + "ms");
        return invoke;
    }
}
